//
//  UIView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/18.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

extension UIView {

    //MARK:返回该view所在的父view
    func getSuperView<T: UIView>(of: T.Type) -> T? {
        for view in sequence(first: self.superview, next: { $0?.superview }) {
            if let father = view as? T {
                return father
            }
        }
        return nil
    }
    
    func findController() -> UIViewController! {
        return self.findControllerWithClass(clzz: UIViewController.self)
    }
    
    func findNavigator() -> UINavigationController! {
            return self.findControllerWithClass(clzz: UINavigationController.classForCoder())
    }
    
    func findControllerWithClass<T>(clzz: AnyClass) -> T? {
        
        var responder = self.next
        while(responder != nil) {
            
            if (responder?.isKind(of: clzz))! {
                return responder as? T
            }
            responder = responder?.next
        }
        
        return nil
    }
    
    //MARK:返回该view size origin 长宽高
    func size() -> CGSize {
        return self.frame.size
    }
    
    func origin() -> CGPoint {
        return self.frame.origin
    }
    
    func x() -> CGFloat {
        return self.frame.origin.x
    }
    
    func y() -> CGFloat {
        return self.frame.origin.y
    }
    
    func width() -> CGFloat {
        return self.frame.size.width
    }
    
    func height() -> CGFloat {
        return self.frame.size.height
    }
    
    func setCircel() { //控件圆形
        self.layer.masksToBounds = true
        self.layer.cornerRadius = self.frame.size.width/2
    }
    
    func setCornerRadius(radius: CGFloat) {  //控件弧度
        self.layer.masksToBounds = true
        self.layer.cornerRadius = radius
    }


}
